选择复选框并将其作为参数发送给操作

发布于 2024-12-15 21:00:43 字数 722 浏览 6 评论 0原文

我正在尝试对项目列表实施大规模删除操作,其中每个项目都有一个复选框,并且有一个按钮用于删除所有选定的项目。顺便说一句,所有这些都在 symfony 中。

我最大的问题是:带有复选框的列表来自 AJAX 调用,因此我无法在定义列表的模板中定义侦听器。我需要在接收 AJAX 响应的模板中执行此操作。

我给你一些我的代码:

  • 返回列表的模板包含:

`

foreach($items as $item){
    echo '<input id="'.$item->getItemID().'" type="checkbox" onClick="[I CAN'T REFERENCE HERE A FUNCTION IN THE OTHER TEMPLATE]">';
    echo 'etc ...';
}`
  • 并且接收列表的模板是

`

<div id="itemList">
    [the AJAX list goes here]  
</div>
<input type="button" value="delete all items">`

所以我的问题是:我怎样才能做到这一点,当按下按钮时,调用 symfony 操作以一组选定的复选框(或等效信息)作为参数。

非常感谢您抽出时间!

i'm trying to implement a massive delete action to a list of items, where every item has a checkbox, and there is a button to delete all the selected items. All that in symfony btw.

My biggest problem is: that list with the checkboxes comes from an AJAX call, so i can't define the listeners in the template where the list is defined. I need to do it in the template that recieves the AJAX response.

i give you some of my code:

  • the template returning the list contains:

`

foreach($items as $item){
    echo '<input id="'.$item->getItemID().'" type="checkbox" onClick="[I CAN'T REFERENCE HERE A FUNCTION IN THE OTHER TEMPLATE]">';
    echo 'etc ...';
}`
  • and the template recieving the list is

`

<div id="itemList">
    [the AJAX list goes here]  
</div>
<input type="button" value="delete all items">`

So my question is: How can i do it to implement that, when the button is pressed, call to for a symfony action with an array of selected checkboxes (or equivalent information) as parameter.

Thank you very much for your time!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

茶色山野 2024-12-22 21:00:43

要做的第一件事是获取复选框数组并将它们传递给 symfony。如果您有以下 html:

<input id="1" type="checkbox" class="delete">
<input id="2" type="checkbox" class="delete">
<input id="3" type="checkbox" class="delete">
<input id="4" type="checkbox" class="delete">
<input type="submit">

和以下 jquery:

$('input[type=submit]').click(function(){
    var elems = new Array();
    $('.delete:checked').each(function(){
        elems.push($(this).attr('id'));
    });

    $.ajax({
        type: 'POST',
        url: 'url',
        data: {elements_to_delete: elems}});
});

请参见此处:http://jsfiddle.net/HbkvF/

然后你执行 executeDeleteBatch() 类似的操作(未测试):

public function execute(sfWebRequest $request)
{
  $ids = $request->getParameter('elements_to_delete');
  $count = Doctrine_Query::create()
    ->delete()
    ->from('<YOUR CLASS>')
    ->whereIn('id', $ids)
    ->execute();
  }
}

$count 返回项目数已删除。

The first thing to do is get the array of checkboxes and pass them to the symfony. If you have the following html:

<input id="1" type="checkbox" class="delete">
<input id="2" type="checkbox" class="delete">
<input id="3" type="checkbox" class="delete">
<input id="4" type="checkbox" class="delete">
<input type="submit">

with the following jquery:

$('input[type=submit]').click(function(){
    var elems = new Array();
    $('.delete:checked').each(function(){
        elems.push($(this).attr('id'));
    });

    $.ajax({
        type: 'POST',
        url: 'url',
        data: {elements_to_delete: elems}});
});

See here: http://jsfiddle.net/HbkvF/

Then you do a executeDeleteBatch() something like (not tested):

public function execute(sfWebRequest $request)
{
  $ids = $request->getParameter('elements_to_delete');
  $count = Doctrine_Query::create()
    ->delete()
    ->from('<YOUR CLASS>')
    ->whereIn('id', $ids)
    ->execute();
  }
}

$count returns the number of items deleted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文